<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/6/22
 * Time: 16:37
 */
namespace Admin\Controller;
use Think\Controller;

class InvoiceManageController extends CommonController
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->breadcrumb1 = '发票管理';

        $this->url = U('Invite/index');
    }

    //待开票
    public function index(){
        $this->breadcrumb2 = '待开发票';
        $name=I('post.name');
        $phone=I('post.phone');
        $number=I('post.number');
        $where=array();
        $where['status']=0;
        $where['is_pay']=1;

        if($name){
            $where['contacts_name']=$name;
        }

        if($phone){
            $where['contacts_phone']=$phone;
        }

        if($number){
            $where['number']=$number;
        }

        if(!empty($where)){
            $count=M('invoice')->where($where)->count();
            $Page       = new \Think\Page($count,25);
            $list=M('invoice')->where($where)
                ->limit($Page->firstRow,$Page->listRows)
                ->field('*,(money+tax+postage) total')
                ->select();
        }else{
            $count=M('invoice')->count();
            $Page       = new \Think\Page($count,25);
            $list=M('invoice')->where($where)
                ->limit($Page->firstRow,$Page->listRows)->field('*,(money+tax+postage) total')->select();
        }

        $this->assign('list',$list);
        $this->display();
    }

    public function op(){
        $id=I('post.id');
        $where['id']=$id;
        $save['status']=1;
        M('invoice')->where($where)->save($save);
        echo 1;
    }

    //已开票列表
    public function invoice(){
        $this->breadcrumb2 = '已开发票';
        $name=I('post.name');
        $phone=I('post.phone');
        $number=I('post.number');
        $where=array();
        $where['status']=array('neq',0);

        if($name){
            $where['contacts_name']=$name;
        }

        if($phone){
            $where['contacts_phone']=$phone;
        }

        if($number){
            $where['number']=$number;
        }

        if(!empty($where)){
            $count=M('invoice')->where($where)->count();
            $Page       = new \Think\Page($count,25);
            $list=M('invoice')->where($where)
                ->limit($Page->firstRow,$Page->listRows)
                ->field('*,(money+tax+postage) total')
                ->select();
        }else{
            $count=M('invoice')->count();
            $Page       = new \Think\Page($count,25);
            $list=M('invoice')->where($where)
                ->limit($Page->firstRow,$Page->listRows)->field('*,(money+tax+postage) total')->select();
        }

        $this->assign('list',$list);
        $this->display();
    }


    //添加邮寄信息
    public function addMessage(){
        $express=I('post.express');     //快递公司
        $express_number=I('post.number');   //快递编号
        $id=I('post.id');
        if(empty($express)){
            echo 2;exit;
        }

        if(empty($express_number)){
            echo 3;exit;
        }

        $save['express']=$express;
        $save['status']=2;
        $save['express_number']=$express_number;
        $where['id']=$id;
        $sa=M('invoice')->where($where)->save($save);
        echo 1;exit;
    }
}